package com.whd.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whd.demo.common.Result;
import com.whd.demo.entity.Register;
import com.whd.demo.mapper.RegisterMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/register")
public class RegisterController {

    @Resource
    RegisterMapper registerMapper;

    /**
     * 新增一条打卡记录
     * @param register
     * @return
     */
    @PostMapping
    public Result<?> save(@RequestBody Register register) {
        registerMapper.insert(register);
        return Result.success();
    }

    /**
     * 分页查询（查询自己的打卡信息）（普通用户专属功能）
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/ownerHouse")
    public Result<?> findPageOwner(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "5") Integer pageSize,
                                   @RequestParam int userId){
        LambdaQueryWrapper<Register> wrapper = Wrappers.<Register>lambdaQuery().eq(Register::getUserId,userId);
        Page<Register> registerPage = registerMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(registerPage);
    }

    /**
     * 分页查询（查询某房主的房屋信息）（管理员专属功能）
     * @param pageNum
     * @param pageSize
     * @param userId
     * @return
     */
    @GetMapping("/selectHouse")
    public Result<?> selectHouseByOwnerId(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "5") Integer pageSize,
                                          @RequestParam Integer userId){
        LambdaQueryWrapper<Register> wrapper = Wrappers.<Register>lambdaQuery().eq(Register::getUserId,userId);
        Page<Register> registerPage = registerMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(registerPage);
    }

    /**
     * 根据房子id查询房子信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(registerMapper.selectById(id));
    }


    /**
     * 删除房屋
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id){
        registerMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 更新房屋信息
     * @param register
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestBody Register register){
        registerMapper.updateById(register);
        return Result.success();
    }

}
